"""嵌入向量生成服务"""
from typing import List
from openai import AsyncOpenAI
from app.core.config import settings


class EmbeddingService:
    """嵌入向量生成服务"""
    
    def __init__(self):
        """初始化 OpenAI 客户端"""
        self.client = AsyncOpenAI(
            api_key=settings.OPENAI_API_KEY,
            base_url=settings.OPENAI_BASE_URL
        )
        self.model = settings.EMBEDDING_MODEL
    
    async def generate_embedding(self, text: str) -> List[float]:
        """
        为单个文本生成嵌入向量
        
        Args:
            text: 待生成嵌入向量的文本
            
        Returns:
            嵌入向量
        """
        response = await self.client.embeddings.create(
            model=self.model,
            input=text
        )
        return response.data[0].embedding
    
    async def generate_embeddings_batch(self, texts: List[str]) -> List[List[float]]:
        """
        批量生成嵌入向量
        
        Args:
            texts: 待生成嵌入向量的文本列表
            
        Returns:
            嵌入向量列表
        """
        response = await self.client.embeddings.create(
            model=self.model,
            input=texts
        )
        return [item.embedding for item in response.data]

